<?php

class Admin_Model_Widget
{
	public static function activate($idname, $active = TRUE)
	{
		if (empty($idname)) {
			throw new Exception('Invalid id name');
		}

		$componentDbTable = new Euso_Model_DbTable_WidgetRegistry();

		$dbData = array(
			'active' => $active ? 1 : 0,
		);

		$updRows = $componentDbTable->update($dbData, array('idname= ?' => $idname));

		if ($updRows != 1) {
			$widget = Kernel::getInstance()->getWidget($idname, FALSE);
			if ($widget instanceof Euso_Widget) {
				$dbData['idname'] = $idname;
				$dbData['version'] = $widget->getVersion();
				$insetId = $componentDbTable->insert($dbData);
				if ($insetId < 1) {
					throw new Exception('Query error');
				}
			} else {
				throw new Exception('Widget not found');
			}
		}
	}
}
